#include <mysql/mysql.h>
#include <string.h>
#include <stdio.h>

int get_id_and_isdir_by_name(MYSQL* conn, const char* name, int p_id,char*user_name, int* out_id, int* out_is_dir) {
    MYSQL_STMT *stmt = mysql_stmt_init(conn);
    if (!stmt) {
        fprintf(stderr, "mysql_stmt_init failed\n");
        return -1;
    }

    const char* query = "SELECT id, is_dir FROM nodes WHERE name=? AND p_id=? AND user_name=?";
    if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0) {
        fprintf(stderr, "mysql_stmt_prepare failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

   MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));

    // 参数绑定：name
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (void*)name;
    bind[0].buffer_length = strlen(name);

    // 参数绑定：p_id
    bind[1].buffer_type = MYSQL_TYPE_LONG;
    bind[1].buffer = (void*)&p_id;
    //user_name
     bind[2].buffer_type = MYSQL_TYPE_STRING;
    bind[2].buffer = (void*)user_name;
    bind[2].buffer_length = strlen(user_name);

    if (mysql_stmt_bind_param(stmt, bind) != 0) {
        fprintf(stderr, "mysql_stmt_bind_param failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    if (mysql_stmt_execute(stmt) != 0) {
        fprintf(stderr, "mysql_stmt_execute failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    // 绑定结果：id 和 is_dir
    MYSQL_BIND result[2];
    memset(result, 0, sizeof(result));

    result[0].buffer_type = MYSQL_TYPE_LONG;
    result[0].buffer = (void*)out_id;
    result[0].is_null = 0;

    result[1].buffer_type = MYSQL_TYPE_LONG;
    result[1].buffer = (void*)out_is_dir;
    result[1].is_null = 0;

    if (mysql_stmt_bind_result(stmt, result) != 0) {
        fprintf(stderr, "mysql_stmt_bind_result failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    int status = mysql_stmt_fetch(stmt);
    mysql_stmt_close(stmt);

    if (status == 0) {
        return 0;  // 成功找到 id 和 is_dir
    } else {
        return -1; // 没找到或错误
    }
}

MYSQL* connect_mysql() {
    MYSQL *conn = mysql_init(NULL);
    if (conn == NULL) {
        fprintf(stderr, "mysql_init failed\n");
        exit(EXIT_FAILURE);
    }

    if (mysql_real_connect(conn,
                           "127.0.0.1",   // 服务器地址
                           "root",             // 用户名
                           "kingyh1@",         // 密码
                           "project",          // 数据库名
                           3306,               // 端口号，默认3306
                           NULL,               // unix_socket，一般用NULL
                           0                   // client_flag，一般用0
                          ) == NULL) {
        fprintf(stderr, "mysql_real_connect failed: %s\n", mysql_error(conn));
        mysql_close(conn);
        exit(EXIT_FAILURE);
    }

    return conn;
}
int get_user_root_id(MYSQL* conn, const char* user_name, int* root_id) {
    int is_dir;
    // 假设你的根目录名字就是 "/"
    return get_id_and_isdir_by_name(conn, "/", -1, (char*)user_name, root_id, &is_dir);
}
int create_table(MYSQL *conn) {
    const char *create_sql =
        "CREATE TABLE IF NOT EXISTS nodes ("
        "id INT AUTO_INCREMENT PRIMARY KEY,"
        "name VARCHAR(255) NOT NULL,"
        "hash VARCHAR(255),"
        "p_id INT,"
        "is_dir INT NOT NULL,"
        "user_name VARCHAR(255) NOT NULL"
        ");";

    if (mysql_query(conn, create_sql)) {
        fprintf(stderr, "创建表失败: %s\n", mysql_error(conn));
        return -1;
    }
    return 0;
}

int get_name_by_id(MYSQL* conn, int id, char* name_out, int out_size) {
    MYSQL_STMT* stmt = mysql_stmt_init(conn);
    if (!stmt) return -1;

    const char* query = "SELECT name FROM nodes WHERE id = ?";
    if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0) {
        mysql_stmt_close(stmt);
        return -1;
    }

    MYSQL_BIND bind_param[1];
    memset(bind_param, 0, sizeof(bind_param));
    bind_param[0].buffer_type = MYSQL_TYPE_LONG;
    bind_param[0].buffer = (void*)&id;

    if (mysql_stmt_bind_param(stmt, bind_param) != 0) {
        mysql_stmt_close(stmt);
        return -1;
    }

    if (mysql_stmt_execute(stmt) != 0) {
        mysql_stmt_close(stmt);
        return -1;
    }

    MYSQL_BIND bind_result[1];
    memset(bind_result, 0, sizeof(bind_result));
    bind_result[0].buffer_type = MYSQL_TYPE_STRING;
    bind_result[0].buffer = name_out;
    bind_result[0].buffer_length = out_size;

    if (mysql_stmt_bind_result(stmt, bind_result) != 0) {
        mysql_stmt_close(stmt);
        return -1;
    }

    int status = mysql_stmt_fetch(stmt);
    mysql_stmt_close(stmt);

    return (status == 0) ? 0 : -1;
}
